"""
用于操作题库数据
"""
import socket
from random import shuffle
 
from pfExcel import ExcelWork
 
# 问题设置
QUESTION_CONFIG = {
    'radioNum': 1,
    'checkboxNum': 1,
    'trueOrFalseNum': 1,
    'radioResult': 1,
    'checkboxResult': 1,
    'trueOrFalseResult': 1
}
 
 
def loadQb(qbPath):
    """
    加载题库数据
    :param qbPath: list: 题库地址
    :return: list: [[单选题,], [多选题,], [判断题,]]
    """
    # 单选题列表
    radioList = []
    # 多选题列表
    checkboxList = []
    # 判断题列表
    trueOrFalseList = []
    # 读取excel
    excel = ExcelWork(qbPath)
    # 获取所有数据
    for index, _ in enumerate(excel.getColumn(1)):
        # 每行数据: ['索引', '题型', '题目', '选项A', '选项B', '选项C', '选项D', '选项E', '选项F', '答案']
        rowList = excel.getRow(index + 1)[0:10]
        # 根据题型划分
        if rowList[1] == '单选题':
            radioList.append(rowList)
        elif rowList[1] == '多选题':
            checkboxList.append(rowList)
        elif rowList[1] == '判断题':
            trueOrFalseList.append(rowList)
    return [radioList, checkboxList, trueOrFalseList]
 
 
def randomQbData(qbData):
    """
    随机题库数据直接改变原始数据
    :param qbData: list: [[单选题,], [多选题,], [判断题,]]
    :return:
    """
    for tempData in qbData:
        # 打乱列表数据的顺序
        shuffle(tempData)
 
 
def getUserQbData(qbData):
    """
    获取用户题库数据并保存
    :param qbData: list: [[单选题,], [多选题,], [判断题,]]
    :return: list: [[题],]
    """
    radioList = qbData[0][:int(QUESTION_CONFIG['radioNum'])]
    checkboxList = qbData[1][:int(QUESTION_CONFIG['checkboxNum'])]
    trueOrFalseList = qbData[2][:int(QUESTION_CONFIG['trueOrFalseNum'])]
    return radioList + checkboxList + trueOrFalseList
 
 
def listToStr(dataList):
    """
    将列表中的值提取出来转为字符串
    :param dataList: list
    :return: str
    """
    tempStr = ''
    for temp in dataList:
        tempStr = tempStr + str(temp)
    return tempStr
 
 
def checkQbData(userQbData):
    """
    检查答案与用户作答数据进行比对为列表添加结果
    :param userQbData: list: [[索引, 题型, 题目, 选项A, 选项B, 选项C, 选项D, 选项E, 选项F, 答案, 用户作答],]
    :return: list: [[索引, 题型, 题目, 选项A, 选项B, 选项C, 选项D, 选项E, 选项F, 答案, 用户作答, 正确|错误],]
    """
    for qb in userQbData:
        # 比对结果
        if qb[9] == qb[10]:
            qb.append('正确')
        else:
            qb.append('错误')
 
 
def getUserResult(userQbData):
    """
    获取用户成绩
    :param userQbData: list: [[索引, 题型, 题目, 选项A, 选项B, 选项C, 选项D, 选项E, 选项F, 答案, 用户作答, 正确|错误],]
    :return: float: 用户成绩
    """
    result = 0
    for temp in userQbData:
        if temp[-1] == '正确':
            if temp[1] == '单选题':
                result = result + float(QUESTION_CONFIG['radioResult'])
            elif temp[1] == '多选题':
                result = result + float(QUESTION_CONFIG['checkboxResult'])
            elif temp[1] == '判断题':
                result = result + float(QUESTION_CONFIG['trueOrFalseResult'])
    return result
 
 
def getIp():
    """
    查询本机ip地址
    :return: str: 本机ip
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('8.8.8.8', 80))
    ip = s.getsockname()[0]
    s.close()
    return ip